using System;
using System.Collections.Generic;
using System.Text;
using ServiceStack.Redis;

using GameTools;

namespace DTFight
{
	public class SSRedisClient
	{
		static SSRedisClient mInstance = new SSRedisClient();
		static public SSRedisClient Instance() { return mInstance; }

        public enum DB_TYPE
        {
            CHARACTER = 0,
            STATISTICS,
        }
		
		ServiceStack.Redis.IRedisClient[]	mRedisServer = null;//new RedisClient[16];
		ServiceStack.Redis.IRedisClient	mRedisDB = null;
		ServiceStack.Redis.IRedisClient  mRedisBusiness = null;
		ServiceStack.Redis.IRedisClient  mRedisStatistics = null;
		ServiceStack.Redis.IRedisClient[] mRedisCommunication = null;//new RedisClient[16];
		ServiceStack.Redis.IRedisClient[] mRedisPayment = null;//new RedisClient[16];

        public string ServerID = "10001";
        public string ServerName = "一区";
		public string ServerDBIP = "127.0.0.1";
        public int ServerDBPort = 3680;

		public bool TestMode = false;

		public void Connect()
		{
//            ServerName = ConfigMgr.GetValue ("Server Name");
//            ServerID = ConfigMgr.GetValue ("Server ID");
//
//			ServerDBIP = ConfigMgr.GetValue ("ServerDB IP");
//			ServerDBPort = int.Parse(ConfigMgr.GetValue ("ServerDB Port"));
//            for (int i = 0; i < mRedisServer.Length; ++i)
//            {
//                mRedisServer[i] = CreateServerDBRedis();
//				mRedisServer [i].Db = i;
//            }
//            Console.WriteLine("Connect ServerDB Successful!");
//
//			string ip = ConfigMgr.GetValue ("ShareDB IP");
//			int port = int.Parse(ConfigMgr.GetValue ("ShareDB Port"));
//			mRedisDB = new ServiceStack.Redis.RedisClient (ip, port);
//            Console.WriteLine("Connect ShareDB Successful!");
//
//            ip = ConfigMgr.GetValue ("BusinessDB IP");
//            port = int.Parse(ConfigMgr.GetValue ("BusinessDB Port"));
//            mRedisBusiness = new ServiceStack.Redis.RedisClient (ip, port);
//            Console.WriteLine("Connect BusinessDB Successful!");
//
//            ip = ConfigMgr.GetValue ("StatisticsDB IP");
//            port = int.Parse(ConfigMgr.GetValue ("StatisticsDB Port"));
//            mRedisStatistics = new ServiceStack.Redis.RedisClient (ip, port);
//            Console.WriteLine("Connect StatisticsDB Successful!");
//
//            ip = ConfigMgr.GetValue ("CommunicationDB IP");
//            port = int.Parse(ConfigMgr.GetValue ("CommunicationDB Port"));
//            for (int i = 0; i < mRedisCommunication.Length; ++i)
//            {
//                mRedisCommunication[i] = new ServiceStack.Redis.RedisClient (ip, port);
//				mRedisCommunication[i].Db = i;
//            }
//            Console.WriteLine("Connect CommunicationDB Successful!");
//
//            ip = ConfigMgr.GetValue ("PaymentDB IP");
//            port = int.Parse(ConfigMgr.GetValue ("PaymentDB Port"));
//            for (int i = 0; i < mRedisPayment.Length; ++i)
//            {
//                mRedisPayment[i] = new ServiceStack.Redis.RedisClient (ip, port);
//				mRedisPayment[i].Db = i;
//            }
//            Console.WriteLine("Connect PaymentDB Successful!");
//
//			TestMode = bool.Parse(ConfigMgr.GetValue ("TestMode"));
		}

		public ServiceStack.Redis.IRedisClient CreateServerDBRedis()
		{
			//return new ServiceStack.Redis.RedisClient (ServerDBIP, ServerDBPort);
			return null;
		}

		public ServiceStack.Redis.IRedisClient GetRedis(int idx)
		{
            if (idx < 0 || idx >= mRedisServer.Length)
                return null;

            return mRedisServer[idx];
		}

		public ServiceStack.Redis.IRedisClient GetRedisDB()
		{
			return mRedisDB;
		}

		public ServiceStack.Redis.IRedisClient GetRedisBusiness()
        {
            return mRedisBusiness;
        }

		public ServiceStack.Redis.IRedisClient GetRedisStatistics()
        {
            return mRedisStatistics;
        }

		public ServiceStack.Redis.IRedisClient GetRedisCommunication(int idx)
        {
            if (idx < 0 || idx >= mRedisCommunication.Length)
                return null;

            return mRedisCommunication[idx];
        }

		public ServiceStack.Redis.IRedisClient GetRedisPayment(int idx)
        {
            if (idx < 0 || idx >= mRedisPayment.Length)
                return null;

            return mRedisPayment[idx];
        }

		static public byte[] StringToByte(string srcStr)
		{
			return Encoding.UTF8.GetBytes(srcStr);
		}
		
		static public string ByteToString(byte[] srcByteStr)
		{
			if (srcByteStr == null)
				return "";

			return Encoding.UTF8.GetString(srcByteStr);
		}

		public bool GetHVals(ServiceStack.Redis.IRedisClient redis, string keyName, ref Dictionary<string, string> atts)
		{
			if (!redis.ContainsKey (keyName))
				return false;

			if (atts == null)
				atts = new Dictionary<string, string>();

//			byte[][] data = redis.HGetAll (keyName);
//			for (int i=0, count = data.Length; i < count; ++i) {
//				string key = Encoding.UTF8.GetString(data[i]);
//				string val = Encoding.UTF8.GetString(data[++i]);
//				atts.Add(key, val);
//			}

			return true;
		}

		public void SetHVals(ServiceStack.Redis.IRedisClient redis, string keyName, Dictionary<string, string> atts)
		{
            if (atts == null || atts.Count == 0)	return;
			if (keyName.Equals (""))	return;

			byte[][] mkeys = new byte[atts.Count][];
			byte[][] mvals = new byte[atts.Count][];
			int i = 0;
			foreach (var att in atts) {
				mkeys[i] = Encoding.UTF8.GetBytes (att.Key);
				mvals [i] = Encoding.UTF8.GetBytes (att.Value);
				++i;
			}

			//redis.HMSet(keyName, mkeys, mvals);
		}

		public void DictionaryToBytes(Dictionary<string, string> atts, ref byte[][] keys, ref byte[][] vals)
		{
			keys = new byte[atts.Count][];
			vals = new byte[atts.Count][];
			int i = 0;
			foreach (var att in atts) {
				keys[i] = Encoding.UTF8.GetBytes (att.Key);
				vals [i] = Encoding.UTF8.GetBytes (att.Value);
				++i;
			}
		}

		public void BytesToDictionary(byte[][] keys, byte[][] vals, ref Dictionary<string, string> atts)
		{
			if (keys.Length != vals.Length)
				return;

			if (atts == null)
				atts = new Dictionary<string, string>();

			for (int i=0, count = keys.Length; i < count; ++i) {
				string key = Encoding.UTF8.GetString(keys[i]);
				string val = Encoding.UTF8.GetString(vals[++i]);
				atts.Add(key, val);
			}
		}

        /// <summary>
        /// Unix时间戳转成时间
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime FROM_UNIXTIME(long timeStamp)
        {
            DateTime date = DateTime.Parse("1970-01-01 00:00:00").AddSeconds(timeStamp);
            return TimeZone.CurrentTimeZone.ToLocalTime(date);
        }

        /// <summary>
        /// 时间转成Unix时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static long TO_UNIXTIME(DateTime dateTime)
        {
            return (dateTime.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-01-01 00:00:00")).Ticks) / 10000000;
        }
	}
}

